﻿using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace ChangeAllName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string filePath = this.folderPath.Text;
            string replace = oldStr.Text;
            string newString = newStr.Text;

            if (string.IsNullOrEmpty(filePath))
            {
                ShowLog("请选择路径");
                return;
            }

            DirectoryInfo directory = new DirectoryInfo(filePath);
            DirectoryInfo[] dirs = directory.GetDirectories();
            if (dirs.Length <= 0)
            {
                ShowLog("没有文件夹存在");
                return;
            }

            foreach (var dir in dirs)
            {
                ChangeDirectoryName(dir, replace, newString);
                ShowLog(dir.FullName);
            }
        }

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
                    this.folderPath.Text = this.folderBrowserDialog1.SelectedPath.Trim();
            }
        }



        private void ChangeDirectoryName(DirectoryInfo currentDir, string replaceName, string newString)
        {
            DirectoryInfo[] dirs = currentDir.GetDirectories();
            string notMeAllPath = currentDir.FullName;
            int last斜杠 = notMeAllPath.LastIndexOf("\\", StringComparison.Ordinal);

            string lastDirPath = notMeAllPath.Substring(last斜杠).ReplaceRegex(replaceName, newString);
            string newPath = notMeAllPath.Substring(0, last斜杠) + "\\" + lastDirPath;

            FileInfo[] files = currentDir.GetFiles();
            if (files.Length > 0)
            {
                foreach (var fileInfo in files)
                {
                    ChangeFileName(fileInfo, replaceName, newString);
                }
            }

            if (dirs.Length > 0)
            {
                foreach (var dir in dirs)
                {
                    ChangeDirectoryName(dir, replaceName, newString);
                }
            }

            //最后改文件夹名称
            if (!Directory.Exists(newPath))
            {
                ShowLog(currentDir.FullName);
                currentDir.MoveTo(newPath);

            }

        }

        private void ChangeFileName(FileInfo currentFile, string replaceName, string newString)
        {
            string newFileName = currentFile.Name.ReplaceRegex(replaceName, newString);
            string newPathName = currentFile.FullName.ReplaceRegex(currentFile.Name, newFileName);

            ReplaceFileContent(currentFile, replaceName, newString);

            if (!System.IO.File.Exists(newPathName))
            {
                ShowLog(currentFile.FullName);
                currentFile.MoveTo(newPathName);
            }
        }

        private void ReplaceFileContent(FileInfo file, string replaceName, string newString)
        {

            ShowLog("替换里面的内容" + file.FullName);
            string content = System.IO.File.ReadAllText(file.FullName);
            content = content.ReplaceRegex(replaceName, newString);
            System.IO.File.WriteAllText(file.FullName, content,Encoding.Unicode);
        }

        private void ShowLog(string text)
        {
            result.Items.Add(text);
        }
    }

    public static class RegexReplace
    {
        public static string ReplaceRegex(this string inputStr, string pattern, string replaceValue)
        {
            Regex regex = new Regex(pattern);
            return regex.Replace(inputStr, replaceValue);
        }
    }
}
